<?php
/*
    
    Copyright (C)  2012 Sweta ray.
    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 1.3
    or any later version published by the Free Software Foundation;
    with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
    A copy of the license is included in the section entitled "GNU
    Free Documentation License"
	@license GNU/GPL http://www.gnu.org/copyleft/gpl.html
    Questions for Joomla
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
   
	Questions for Joomla
	Version 0.0.1
	Created date: Sept 2012
	Creator: Sweta Ray
	Email: admin@phpseo.net
	support: support@phpseo.net
	Website: http://www.phpseo.net
*/

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');

class QuestionsViewQuestion extends QueView
{
        // Overwriting JView display method
        function display($tpl = null) 
        {
        	global $logger;
            
        	$user = JFactory::getUser();
        	$app = JFactory::getApplication();
        	
        	$this->question = $this->get("Item");

        	//Authorizations
        	$user = JFactory::getUser();
        	$this->assignRef("viewanswers", $user->authorise("question.viewanswers" , "com_questions"));
        	$this->assignRef("submitanswers", $user->authorise("question.answer" , "com_questions"));
        	
        	//params
        	$params = $app->getParams();
        	$this->assignRef("params", $params);
        	$this->assignRef("pageclass_sfx" , htmlspecialchars($params->get('pageclass_sfx')));
        	
        	//check ownership
        	$isOwner = (bool)($user->id == $this->question->userid_creator && $user->id != 0 );
        	$this->assignRef("isOwner", $isOwner);  

        	//Add Pathway
        	QuestionsHelper::addPathway(); 
        	$pathway=$app->getPathway();
        	$pathway->addItem ( $this->question->title );
        	
        	if ( @$this->question && $this->question->viewable ){ //check for questions, suppressing errors..
	        		parent::display($tpl);

        	}
        	else{
				//$app =& JFactory::getApplication();
				//$app->redirect('index.php?option=com_questions&view=form&layout=edit');
        		$logger->error("No Results..");
        		JError::raiseNotice(404, JText::_("COM_QUESTIONS_ERROR_404"));
        	}
        }
		function getCategories(){
					$limit = $this->params->get('cat_limit', 5);
					$ordering = $this->params->get('cat_ordering', 'c.id');
					$db = JFactory::getDBO();
					$query = $db->getQuery(true);
					$query->select('c.id, c.parent_id, c.lft, c.rgt, c.level, c.title');
					$query->from('#__categories as c');
					$query->where('c.extension= "com_questions" AND c.published=1');
					$query->order($ordering. ' ASC LIMIT '. $limit);
					$db->setQuery((string)$query);
					$categories = $db->loadObjectList();
					return $categories;
					
		}

		function countCat($catid){
					$this->catid = $catid;
					$db = JFactory::getDBO();
					$query = $db->getQuery(true);
					$query->select('count(*) FROM #__questions_core AS c WHERE c.catid='.(int)$catid.' AND c.question=1');
					//echo $query;
					$db->setQuery($query);
					$result = $db->loadResult();
					return $result;
	
		}
		
		function nested($left,$right,$lvl)
		{
					$sublimit = $this->params->get('sub_cat_limit', 5);
					$subordering = $this->params->get('sub_cat_ordering', 'c.id');
					$this->left = $left;
					$this->right = $right;
					$this->lvl = $lvl;
					$db = JFactory::getDBO();
					$query = $db->getQuery(true);
					$query->select('c.id, c.parent_id, c.lft, c.rgt, c.level, c.title');
					$query->from('#__categories as c');
					$query->where('c.extension= "com_questions" AND c.published=1 AND c.lft >'. $left.' AND c.rgt <'. $right.' AND c.level= '.$lvl);
					$query->order($subordering. ' ASC LIMIT '. $sublimit);
					$db->setQuery((string)$query);
					$nestedcat = $db->loadObjectList();
     				return $nestedcat;
			}	
			
			function getRank($userid){
					$this->userid = $userid;
					$db = JFactory::getDBO();
					$query = $db->getQuery(true);
					$query->select('rank,userid');
					$query->from('#__questions_userprofile');
					$query->where('userid='.$userid);
					$db->setQuery((string)$query);
					$rank = $db->loadResult();
					return $rank;
					
		   }
		   
		   function getId($userid){
					$rank = $this->getRank($userid);
					$db = JFactory::getDBO();
					$query = $db->getQuery(true);
					$query->select('id');
					$query->from('#__questions_ranks');
					$query->where('rank="'.$rank.'"');
					$db->setQuery((string)$query);
					$image = $db->loadResult();
					return $image;
					
		   }
		   function getChosen($questionid){
					$db = JFactory::getDBO();
					$query = $db->getQuery(true);
					$query->select('count(*)');
					$query->from('#__questions_core');
					$query->where('parent="'.$questionid.'" AND chosen=1');
					$db->setQuery((string)$query);
					$count = $db->loadResult();
					return $count;
					
		   }
		   function getBestAnswerid($questionid){
					$db = JFactory::getDBO();
					$query = $db->getQuery(true);
					$query->select('id');
					$query->from('#__questions_core');
					$query->where('parent="'.$questionid.'" AND chosen=1');
					$db->setQuery((string)$query);
					$count = $db->loadResult();
					return $count;
					
		   }
		   public function getFavourite2($vardata,$userid){
					$db = JFactory::getDBO();
					$query2 = $db->getQuery(true);
					$query2->select("$vardata");
					$query2->from('#__questions_favourite');
					$query2->where('userid='.$userid);
					$db->setQuery((string)$query2);
					$result = $db->loadResult();
					return $result;
					
		   }
		  /* function strip_word_html($text, $allowed_tags = '<b><i><sup><sub><em><strong><u><br>')
    	   {
				mb_regex_encoding('UTF-8');
				//replace MS special characters first
				$search = array('/&lsquo;/u', '/&rsquo;/u', '/&ldquo;/u', '/&rdquo;/u', '/&mdash;/u');
				$replace = array('\'', '\'', '"', '"', '-');
				$text = preg_replace($search, $replace, $text);
				//make sure _all_ html entities are converted to the plain ascii equivalents - it appears
				//in some MS headers, some html entities are encoded and some aren't
				$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
				//try to strip out any C style comments first, since these, embedded in html comments, seem to
				//prevent strip_tags from removing html comments (MS Word introduced combination)
				if(mb_stripos($text, '/*') !== FALSE){
					$text = mb_eregi_replace('#/\*.*?\*/#s', '', $text, 'm');
				/*}
				//introduce a space into any arithmetic expressions that could be caught by strip_tags so that they won't be
				//'<1' becomes '< 1'(note: somewhat application specific)
				$text = preg_replace(array('/<([0-9]+)/'), array('< $1'), $text);
				$text = strip_tags($text, $allowed_tags);
				//eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one
				$text = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $text);
				//strip out inline css and simplify style tags
				$search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu');
				$replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>');
				$text = preg_replace($search, $replace, $text);
				//on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears
				//that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
				//some MS Style Definitions - this last bit gets rid of any leftover comments */
				/*$num_matches = preg_match_all("/\<!--/u", $text, $matches);
				if($num_matches){
					  $text = preg_replace('/\<!--(.)*--\>/isu', '', $text);
				}
				return $text;
		} */
  

}
